package morphage.engine;

public class Pixel
{
  private final static float R_MULTIPLIER = 255.f * 65536.f;
  private final static float G_MULTIPLIER = 255.f * 256.f;
  private final static float B_MULTIPLIER = 255.f;
  private float m_r, m_g, m_b;
  private float m_lum;
  
  public Pixel()
  {
  
  }
  public Pixel( float r, float g, float b )
  {
    setColor( r, g, b );
  }
  public Pixel( int rgb )
  {
    setColor( rgb );
  }
  private float computeLuminosity()
  {
    return ( m_lum = (m_r + m_g + m_b) / 3.f ); 
  }
  public void setColor( float r, float g, float b )
  {
    assert( r >= 0.f && r <= 1.f );
    assert( g >= 0.f && g <= 1.f );
    assert( b >= 0.f && b <= 1.f );
    m_r = r;
    m_g = g;
    m_b = b;
    computeLuminosity();
  }
  public void setColor( int rgb )
  {
    m_r = (float)(rgb & 0x00FF0000) / R_MULTIPLIER;
    m_g = (float)(rgb & 0x0000FF00) / G_MULTIPLIER;
    m_b = (float)(rgb & 0x000000FF) / B_MULTIPLIER;
    float f = (float)(rgb & 0xFF000000);
    computeLuminosity();
  }
  public void setLum( float lum )
  {
    m_r = lum;
    m_g = lum;
    m_b = lum;
    m_lum = lum;
  }
  public int getIntColor()
  {
    assert( m_r >= 0.f );
    assert( m_r <= 1.f );
    assert( m_g >= 0.f );
    assert( m_g <= 1.f );
    assert( m_b >= 0.f );
    assert( m_b <= 1.f );
    return (int)(m_r * R_MULTIPLIER + m_g * G_MULTIPLIER + m_b * B_MULTIPLIER);
  }
  public float r() { return m_r; }
  public float g() { return m_g; }
  public float b() { return m_b; }
  public float lum() { return m_lum; }
}
